<?php

namespace App\Home\Controllers;

use App\Home\Filters\DemoFilter;
use App\Home\Models\Demo;
use App\Home\Requests\DemoRequest;
use App\Home\Resources\DemoResource;
use Illuminate\Http\Request;

class DemoController extends Controller
{
    public function store(
        DemoRequest $request, 
        Demo $demo
    ){
        $inputs = $request->validated();
        $demo = $demo->add($inputs);

        return $this->created(DemoResource::make($demo));
    }

    public function update(
        DemoRequest $request, 
        Demo $demo
    ){
        $inputs = $request->validated();
        $demo->update($inputs);

        return $this->created(DemoResource::make($demo));
    }

    public function destroy(
        Demo $demo
    ){
        $demo->delete();
        return $this->noContent();
    }

    public function index(
        DemoRequest $request,
        DemoFilter $filter
    ){
        $fields = [
            "*",
        ];
        $demos = Demo::query()
            ->filter($filter)
            ->select($fields)
            ->paginate();

        return $this->ok(DemoResource::collection($demos));
    }

    public function show(
        Int $id
    ){
        $fields = [
            "*",
        ];
        $demo = Demo::query()
            ->select($fields)
            ->find($id);
        return $this->ok(DemoResource::make($demo));
    }
}
